package com.ruicar.afs.cloud.workflow.greatwall.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallFlowParams;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallWorkflowMappingInfo;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallFlowParamsService;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowMappingInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.function.Function;

/**
 * The type AfsGreatWallWorkFLowConfigController
 * <p>Description: </p>
 * 长城工作流对接配置
 *
 * @author Fzero
 * @version 1.0
 * @date Created on 2020.07.07 09:42:31
 * @since 1.0
 */
@RestController
@AllArgsConstructor
@Slf4j
@RequestMapping("/workFlowConfig")
public class AfsGreatWallWorkFLowConfigController {

    /**
     * Afs great wall workflow mapping info service
     * <p>Description: </p>
     */
    private final AfsGreatWallWorkflowMappingInfoService afsGreatWallWorkflowMappingInfoService;

    private final AfsGreatWallFlowParamsService afsGreatWallFlowParamsService;

    /**
     * List f low mapping response
     *
     * @param condition condition
     * @return the response
     */
    @PostMapping("/listFlowMapping")
    public IResponse<Page<AfsGreatWallWorkflowMappingInfo>> listFLowMapping(@RequestBody QueryCondition<AfsGreatWallWorkflowMappingInfo> condition) {
        LambdaQueryWrapper<AfsGreatWallWorkflowMappingInfo> queryWrapper = Wrappers.<AfsGreatWallWorkflowMappingInfo>lambdaQuery()
                .eq(StringUtils.isNotEmpty(condition.getCondition().getGreatWallServiceName()), AfsGreatWallWorkflowMappingInfo::getGreatWallServiceName, condition.getCondition().getGreatWallServiceName())
                .eq(StringUtils.isNotEmpty(condition.getCondition().getGreatWallServiceId()), AfsGreatWallWorkflowMappingInfo::getGreatWallServiceId, condition.getCondition().getGreatWallServiceId());
        Page<AfsGreatWallWorkflowMappingInfo> page = afsGreatWallWorkflowMappingInfoService.page(condition.page(), queryWrapper);
        IPage<AfsGreatWallWorkflowMappingInfo> rtnPage =  page.convert(afsGreatWallWorkflowMappingInfo -> {
            afsGreatWallWorkflowMappingInfo.setAutoProcessTypes(Arrays.asList(afsGreatWallWorkflowMappingInfo.getAutoExceptionProcess().split(",")));
            return afsGreatWallWorkflowMappingInfo;
        });
        return IResponse.success(rtnPage);
    }

    /**
     * Save f low mapping response
     *
     * @param workflowMappingInfo workflow mapping info
     * @return the response
     */
    @PostMapping("/saveFlowMapping")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> saveFLowMapping(@RequestBody AfsGreatWallWorkflowMappingInfo workflowMappingInfo) {
        workflowMappingInfo.setAutoExceptionProcess(CollectionUtil.join(workflowMappingInfo.getAutoProcessTypes(),","));
        if (workflowMappingInfo.getId() == null) {
            int count = afsGreatWallWorkflowMappingInfoService.count(Wrappers.<AfsGreatWallWorkflowMappingInfo>lambdaQuery().eq(AfsGreatWallWorkflowMappingInfo::getGreatWallServiceId, workflowMappingInfo.getGreatWallServiceId()));
            Assert.isTrue(count == 0, "流程场景重复");
            afsGreatWallWorkflowMappingInfoService.save(workflowMappingInfo);
        } else {
            AfsGreatWallWorkflowMappingInfo afsGreatWallWorkflowMappingInfo = afsGreatWallWorkflowMappingInfoService.getById(workflowMappingInfo.getId());
            if (!StringUtils.equals(afsGreatWallWorkflowMappingInfo.getGreatWallServiceId(), workflowMappingInfo.getGreatWallServiceId())) {
                afsGreatWallFlowParamsService.update(Wrappers.<AfsGreatWallFlowParams>lambdaUpdate()
                        .set(AfsGreatWallFlowParams::getGreatWallServiceId, workflowMappingInfo.getGreatWallServiceId())
                        .eq(AfsGreatWallFlowParams::getGreatWallServiceId, afsGreatWallWorkflowMappingInfo.getGreatWallServiceId()));
            }
            afsGreatWallWorkflowMappingInfoService.updateById(workflowMappingInfo);
        }
        afsGreatWallWorkflowMappingInfoService.publishFlowMapping();
        return IResponse.success(Boolean.TRUE);
    }

    /**
     * Delete f low mapping response
     *
     * @param workflowMappingInfo workflow mapping info
     * @return the response
     */
    @PostMapping("/deleteFlowMapping")
    public IResponse<Boolean> deleteFLowMapping(@RequestBody AfsGreatWallWorkflowMappingInfo workflowMappingInfo) {
        if (workflowMappingInfo.getId() == null) {
            throw new AfsBaseException(CommonConstants.FAIL, "id为空不能执行删除");
        } else {
            afsGreatWallWorkflowMappingInfoService.removeById(workflowMappingInfo.getId());
        }
        afsGreatWallWorkflowMappingInfoService.publishFlowMapping();
        return IResponse.success(Boolean.TRUE);
    }

}
